<?php
global $_RQ,$_AR;
$merchid=$this->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'coupontype'=>1,'merchid'=>$merchid);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_coupon',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$return['coupontype']=array('1'=>'商户优惠券');
	foreach($return['list'] as &$v){
		$v['createtime']=date('Y/m/d H:i:s',$v['createtime']);
		$getallnum = pdo_count('jy_weishop_coupon_getlog',array('uniacid'=>$_SESSION['uniacid'],'couponid'=>$v['id']));
		if($v['t']<1){
			$v['last']='不限制';
		}else{
			$v['last']=$v['t']-$getallnum;
		}
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商户优惠券列表'),
			array('op'=>'buy','title'=>'添加商户优惠券'),
			//array('op'=>'list2','title'=>'充值优惠券列表'),
			//array('op'=>'recharge','title'=>'添加充值优惠券'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		</br>1、<red>注意优惠券的几种使用限制条件：1、商品分类；2、指定商品；3、是否已过期；4、是否满额；5、是不是多商户的商品；6、是不是多商户的优惠券（多商户的优惠券，只能自己商品使用）。如果条件不满足是不会显示到商品可使用区域的！</red>
		</br>2、本列表不再显示多商户的优惠券；
		</br>3、默认商户的商品不能使用平台的优惠券（可以前往《其他设置》--开启支持）；
		','edit_op'=>'buy','list'=>array(
			array('name'=>'title','title'=>'优惠券名称','type'=>'string'),
			array('name'=>'coupontype','title'=>'优惠券类型','type'=>'radio','options'=>'coupontype'),
			//array('name'=>'logo','title'=>'优惠','type'=>'image'),								
			array('name'=>'last','title'=>'剩余数量','type'=>'string'),
			array('name'=>'status','title'=>'会员中心显示','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
		),
		),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='list2'){
	exi('暂时不支持！','error');
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'coupontype'=>2,'merchid'=>$merchid);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_coupon',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$return['coupontype']=array('1'=>'商户优惠券');
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	foreach($return['list'] as &$v){
		$v['createtime']=date('Y/m/d H:m:s',$v['createtime']);
		$getallnum = pdo_count('jy_weishop_coupon_getlog',array('uniacid'=>$_SESSION['uniacid'],'couponid'=>$v['id']));
		if($v['t']<1){
			$v['last']='不限制';
		}else{
			$v['last']=$v['t']-$getallnum;
		}
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商户优惠券列表'),
			array('op'=>'buy','title'=>'添加商户优惠券'),
			//array('op'=>'list2','title'=>'充值优惠券列表'),
			//array('op'=>'recharge','title'=>'添加充值优惠券'),
		),
		'list'=>array('data'=>'list','edit_op'=>'recharge','list'=>array(
			array('name'=>'title','title'=>'优惠券名称','type'=>'string'),
			array('name'=>'coupontype','title'=>'优惠券类型','type'=>'radio','options'=>'coupontype'),
			//array('name'=>'logo','title'=>'优惠','type'=>'image'),								
			array('name'=>'last','title'=>'剩余数量','type'=>'string'),
			array('name'=>'status','title'=>'会员中心显示','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
		),
		),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='buy'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_coupon',array('id'=>$id));
	}else{
		$item=array();
	}
	$item['ismerch']=intval($item['ismerch']);
	$item['sendtitle']=$item['sendtitle']?$item['sendtitle']:'[nickname]:恭喜您，获得一个优惠券';
	$item['sendimage']=$item['sendimage']?$item['sendimage']:'../core/resource/images/h5game/card.png';
	$item['senddesc']=$item['senddesc']?$item['senddesc']:'优惠券的使用有时间限制，请及时使用！';
	$item['limittimes']=json_decode($item['limittimes'],true);
	$item['sendtime']=json_decode($item['sendtime'],true);
	$setgroupids=array();
	$limitgoodcateids=str_array($item['limitgoodcateids']);
	foreach($limitgoodcateids as $cid){
		$pcate=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$cid,'uniacid'=>$_SESSION['uniacid']),'parentid');
		if(!$pcate){
			$pcate=$cid;
			$cid=0;
		}
		$setgroupids[]=array('pcate'=>$pcate,'ccate'=>$cid);
	}
	$item['setgroupids']=$setgroupids;
	$setgoodsids=array();
	$limitgoodids=str_array($item['limitgoodids']);
	foreach($limitgoodids as $gid){
		$goods=pdo_get('jy_weishop_goods',array('id'=>$gid,'uniacid'=>$_SESSION['uniacid']),array('pcate','ccate'));
		$setgoodsids[]=array('goodsid'=>$gid,'pcate'=>$goods['pcate'],'ccate'=>$goods['ccate']);
	}
	$item['setgoodsids']=$setgoodsids;
	$return['group']=pdo_getall('jy_weishop_coupon_group',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'));
	$goods=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'onepluginname'=>''),array('id','title'),'','id desc');
	foreach($goods as &$v){
		$id=$v['id'];
		$return['goods'][$id]=$v['title'];
	}
	$groups=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'onepluginname'=>'','parentid'=>0),array('id','title'),'','id desc');
	foreach($groups as &$v){
		$id=$v['id'];
		$return['goodgroup'][$id]=$v['title'];
	}
	$member=pdo_getall('jy_weishop_member_level',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('id','title'),'','id desc');
	foreach($member as &$v){
		$id=$v['id'];
		$return['member'][$id]=$v['title'];
	}

	$return['item']=$item;
	$return['member'][0]='默认等级';
	$return['isdescsys']=array('0'=>'不使用','1'=>'使用');
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$return['is']=array('0'=>'否','1'=>'是');
	$return['limitusetype']=array('0'=>'无优惠使用限制','1'=>'不可与促销优惠同时使用','2'=>'不可与会员折扣同时使用','3'=>'不可与促销优惠和会员折扣同时使用');
	$return['limitgoodtype']=array('0'=>'不添加商品限制','1'=>'允许以下商品使用');
	$return['limitgoodcatetype']=array('0'=>'不添加商品分类限制','1'=>'允许以下商品分类使用');
	$return['backtype']=array('0'=>'立减','1'=>'打折','2'=>'返现金');
	$return['timelimittype']=array('0'=>'无限制','1'=>'获得多少天内','2'=>'时间段内');
	$catelevel=2;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'商户优惠券列表'),
			array('op'=>'buy','title'=>'添加商户优惠券'),
		),
		'desc'=>'优惠券设置',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本设置',
				'list'=>array(
					array('type'=>'string','name'=>'paixu','title'=>'排序','help'=>'越大越靠前'),
					array('type'=>'string','name'=>'title','title'=>'优惠券名称'),
					array('type'=>'select','name'=>'groupid','title'=>'优惠券分类','options'=>'group'),
					array('type'=>'image','name'=>'image','title'=>'图片'),
					array('type'=>'string','name'=>'enough','title'=>'使用条件','help'=>'购物满多少可用, 空或0 不限制'),
					array('type'=>'radio','name'=>'timelimittype','title'=>'使用时间限制','options'=>'timelimittype'),
					array('type'=>'string','name'=>'timedays','title'=>'在领取后天数内有效','show'=>array('timelimittype'=>1)),
					array('type'=>'time_range','name'=>'limittimes','title'=>'在时间段内有效','show'=>array('timelimittype'=>2)),	
					array('type'=>'radio','name'=>'backtype','title'=>'优惠方式','options'=>'backtype'),
					array('type'=>'string','name'=>'deduct','title'=>'立减','show'=>array('backtype'=>0)),
					array('type'=>'string','name'=>'discount','title'=>'折扣','help'=>'设置6.8，表示打6.8折','show'=>array('backtype'=>1)),
					array('type'=>'string','name'=>'backmoney','title'=>'返余额','show'=>array('backtype'=>2)),
					array('type'=>'string','name'=>'backcredit','title'=>'返积分','show'=>array('backtype'=>2)),	
					array('type'=>'string','name'=>'backredpack','title'=>'返现金','show'=>array('backtype'=>2)),						
					array('type'=>'string','name'=>'t','title'=>'发放总数','help'=>'0为不限制'),
		)
	),
			array(
				'title'=>'领取设置',
				'list'=>array(
					array('type'=>'radio','name'=>'status','options'=>'status','title'=>'加入领券中心'),
					array('type'=>'switch','name'=>'issendtime','title'=>'限制发放时间'),
					array('type'=>'time_range','name'=>'sendtime','title'=>'发放时间','show'=>array('issendtime'=>1)),	
					array('type'=>'string','name'=>'daymax','title'=>'每人每天限领次数'),
					array('type'=>'string','name'=>'getmax','title'=>'每人限领次数'),
					array('type'=>'string','name'=>'tagtitle','title'=>'标签文字','show'=>array('status'=>1)),
					array('type'=>'radio','name'=>'settagtitlecolor','options'=>'is','title'=>'是否自定义标签颜色','show'=>array('status'=>1)),
					array('type'=>'color','name'=>'titlecolor','title'=>'标签颜色','show'=>array('status'=>1)),
					array('type'=>'switch','name'=>'islimitlevel','title'=>'是否限制会员等级'),
					array('type'=>'checkbox','name'=>'memberlevels','options'=>'member','title'=>'选择可领取会员等级','show'=>array('islimitlevel'=>1)),					
				)
			),
			array(
				'title'=>'使用限制',
				'list'=>array(
					array('type'=>'radio','name'=>'limitusetype','options'=>'limitusetype','title'=>'优惠使用限制'),
					array('type'=>'radio','name'=>'limitgoodcatetype','options'=>'limitgoodcatetype','title'=>'商品分类使用限制','show'=>array('ismerch'=>array(0,2))),	
					array('type'=>'array','show'=>array('limitgoodcatetype'=>1,'ismerch'=>array(0,2)),'name'=>'setgroupids','title'=>'选择商品分类','add_tip'=>'添加一个分类',
						'list'=>array(
							array('type'=>'select','name'=>'pcate','options'=>'allgroups','title'=>'一级分类'),
							array('type'=>'select','name'=>'ccate','options'=>'allgroups','title'=>'二级分类'),
						),
						'details'=>array(
							array('type'=>'select'.$catelevel.'_line','name'=>'pcate','options'=>'pcates','title'=>'选择分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'二级分类',
							'name3'=>'scate','options3'=>'scates','title3'=>'三级分类',
							),
						)
					),
					array('type'=>'radio','name'=>'limitgoodtype','options'=>'limitgoodtype','title'=>'商品使用限制','show'=>array('ismerch'=>array(0,2))),
					array('type'=>'array','show'=>array('limitgoodtype'=>1,'ismerch'=>array(0,2)),'name'=>'setgoodsids','title'=>'选择商品','add_tip'=>'添加一个商品',
						'list'=>array(
							array('type'=>'select','name'=>'pcate','options'=>'allgroups','title'=>'一级分类'),
							array('type'=>'select','name'=>'ccate','options'=>'allgroups','title'=>'二级分类'),
							array('type'=>'select','name'=>'goodsid','options'=>'goods','title'=>'商品名称'),
						),
						'details'=>array(
							array('type'=>'select3_line','name'=>'pcate','options'=>'pcategoods','title'=>'选择分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'二级分类',
							'name3'=>'goodsid','options3'=>'goods','title3'=>'请选择商品',
							),
						)
					),
								
				)
			),
			array(
				'title'=>'推送',
				'list'=>array(
					array('type'=>'string','name'=>'sendtitle','title'=>'推送标题','help'=>'变量 [nickname] 会员昵称 [total] 优惠券张数'),
					array('type'=>'image','name'=>'sendimage','title'=>'推送封面'),
					array('type'=>'string','name'=>'senddesc','title'=>'推送说明','help'=>'变量 [nickname] 会员昵称 [total] 优惠券张数'),	
				)
			),
			array(
				'title'=>'使用说明',
				'list'=>array(
				
					array('type'=>'radio','name'=>'isdescsys','options'=>'isdescsys','title'=>'是否使用统一说明','help'=>'统一说明在【基础设置】中设置，如果使用统一说明，则在优惠券说明前面显示统一说明'),
					array('type'=>'string','name'=>'desc','title'=>'使用说明','help'=>'','show'=>array('isdescsys'=>0)),					
				)
			),
	)
	);
	//获取商品
	$goodsspram=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);

	$return['goods']=pdo_getall('jy_weishop_goods',$goodsspram,array('id','title'),'id');
	//获取分类
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'parentid'=>0);

	$return['pcates']=$return['pcategoods']=pdo_getall('jy_weishop_goods_group',$groupspram,array('title','id'),'id');
	foreach($return['pcates'] as &$pv){
			$pv['ccates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$pv['id']),array('title','id'),'id');
			foreach($pv['ccates'] as &$cv){
				$cv['scates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$cv['id']),array('title','id'),'id');
			}
	}
	foreach($return['pcategoods'] as &$gv){
			$gv['ccates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$gv['id']),array('title','id'),'id');
			$gv['ccates'][0]=array('id'=>0,'title'=>'默认分类');
			$gv['ccates'][0]['goods']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'ccate'=>0,'pcate2'=>$gv['id']),array('title','id'),'id');
			foreach($gv['ccates'] as &$gcv){
				$gcv['goods']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'ccate'=>$gcv['id'],'pcate2'=>$gv['id']),array('title','id'),'id');
			}
	}
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	$return['allgroups']=pdo_getall('jy_weishop_goods_group',$groupspram,array('title','id'),'id');

	$return['merchtype']=array('全商户','部分商户');
	$return['ismerch']=array('平台优惠券','多商户优惠券','共用优惠券');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array_fields(array('deduct','issendtime','sendtime','paixu','title','bgcolor','daymax','getmax','memberlevels','backtype','gettype','image','groupid','enough','timelimittype','tagtitle','settagtitlecolor','titlecolor','islimitlevel','timedays','limittimes','coupontype','discount','backmoney','backcredit','backredpack','t','status','sendtitle','senddesc','sendimage','desc','isdescsys','limitgoodcatetype','limitgoodtype','limitusetype','limitgoodcateids','limitgoodids'),$postdata);
	if($data['t']==0){
		//$data['t']=-1;
	}
	$limitgoodcateids=';';
	$setgroupids=$postdata['setgroupids'];
	foreach($setgroupids as $sv){
		$cid=$sv['ccate']?:$sv['pcate'];
		if(strpos($limitgoodcateids,';'.$cid.';') === false){
			$limitgoodcateids .=$cid.';';
		}
	}
	$data['limitgoodcateids']=$limitgoodcateids;
	$limitgoodids=';';
	$setgoodsids=$postdata['setgoodsids'];
	foreach($setgoodsids as $sv){
		$goodsid=$sv['goodsid'];
		if(strpos($limitgoodids,';'.$goodsid.';') === false){
			$limitgoodids .=$goodsid.';';
		}
	}
	$data['limitgoodids']=$limitgoodids;
	if($data['backtype']==1){
		if(!is_numeric($data['discount'])){
			exi('折扣必须是整数或小数！','error');
		}elseif($data['discount']<=0.1 || $data['discount']>=10){
			exi('折扣必须是大于0，小于10！','error');
		}
	}
	$data['limittimes']=ijson_encode($data['limittimes']);
	$data['sendtime']=ijson_encode($data['sendtime']);

	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_weishop_coupon',$data,array('id'=>$id));
				exi('设置成功！','','list');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				$data['createtime']=TIMESTAMP;
				$data['coupontype']=1;
				$data['last']=$data['t'];
				$data['merchid']=$merchid;
				pdo_insert('jy_weishop_coupon',$data);
				exi('添加成功！','','list');
			}
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_coupon',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_coupon',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>